<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\StreamedResponse;

class SseController extends Controller
{
    //sse index
    public function index(){
        $tdk['title'] = 'chatgpt - 代码示例网';
        $tdk['keywords'] = 'chatgpt';
        $tdk['description'] = 'chatgpt';
        $fronts['tdk'] = $tdk;

        return view('front/tools/chatgptindex',$fronts);
    }

    public function chatgptsse()
    {
        $data = [
            'message' => 'hell word',
            'time' => date('h:i:s')
        ];

        $response = new StreamedResponse();
        $response->setCallback(function () use ($data){

            echo 'data: ' . json_encode($data) . "\n\n";
            ob_flush();
            flush();
            usleep(200000);
        });

        $response->headers->set('Content-Type', 'text/event-stream');
        $response->headers->set('X-Accel-Buffering', 'no');
        $response->headers->set('Cach-Control', 'no-cache');
        $response->send();
    }




}
